//
//  LGshotrDetailController.m
//  lgdw
//
//  Created by mac on 16/1/17.
//  Copyright © 2016年 Apple. All rights reserved.
//

#import "LGshotrDetailController.h"
#import "lgshotr.h"
@interface LGshotrDetailController ()<UIWebViewDelegate>
@property (nonatomic , strong) UIWebView *webView;
/** 遮罩view */
@property (nonatomic , weak) UIView *coverView;
/** 加载指示器 */
@property (nonatomic , weak) UIActivityIndicatorView *loadingView;
@end

@implementation LGshotrDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.webView];
    
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://m.duwu.me/p/%@.html",self.shotrModel.sid]];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:request];
    // 设置代理
    self.webView.delegate = self;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

#pragma -mark /***************** 代理方法 *****************/

- (void)webViewDidStartLoad:(UIWebView *)webView {
    UIView *coverView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    coverView.backgroundColor = [UIColor lightGrayColor];
    [coverView addSubview:self.loadingView];
    [self.loadingView startAnimating];
    self.coverView = coverView;
    
    self.webView.hidden = YES;
    self.webView.scrollView.hidden = YES;
    [self.view addSubview:self.coverView];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    /** 导航  */
    NSString *navgationJS1 = @"document.getElementsByClassName('inner clearfix')[0].remove();";
    NSString *navgationJS = @"document.getElementsByClassName('am-g')[0].remove();";
    [webView stringByEvaluatingJavaScriptFromString:navgationJS];
    [webView stringByEvaluatingJavaScriptFromString:navgationJS1];
    /** 打开 */
    NSString *openJS = @"document.getElementsByClassName('to-app')[0].remove();";
    [webView stringByEvaluatingJavaScriptFromString:openJS];
    
    /** 脚 */
    NSString *footJS = @"document.getElementsByClassName('footer')[0].remove();";
    [webView stringByEvaluatingJavaScriptFromString:footJS];
    
    // 延迟显示信息
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.05 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        self.webView.hidden = NO;
        self.webView.scrollView.hidden = NO;
        self.coverView.hidden = YES;
    });
}



#pragma -mark /***************** 懒加载 *****************/
- (UIWebView *)webView {
    if (_webView == nil) {
        _webView = [[UIWebView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    }
    return _webView;
}
/** 加载指示器 */
- (UIActivityIndicatorView *)loadingView {
    if (!_loadingView) {
        UIActivityIndicatorView *loadingView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        [self.webView addSubview:loadingView];
        
        // 居中
        loadingView.center = self.view.center;
        self.loadingView = loadingView;
    }
    return _loadingView;
}

@end
